import Vue from 'vue'
// v-dialogDrag: 弹窗拖拽
Vue.directive('dialogDrag', {
    bind(el) {
        const dialogHeaderEl = el.querySelector('.el-dialog__header');
        const dragDom = el.querySelector('.el-dialog');
        dialogHeaderEl.style.cursor = 'move';
        const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null);
        dialogHeaderEl.onmousedown = (e) => {
            const disX = e.clientX - dialogHeaderEl.offsetLeft;
            const disY = e.clientY - dialogHeaderEl.offsetTop;
            let styL, styT;
            if (sty.left.includes('%')) {
                // eslint-disable-next-line no-useless-escape
                styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100);
                // eslint-disable-next-line no-useless-escape
                styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100);
            } else {
                styL = +sty.left.replace(/\px/g, '');
                styT = +sty.top.replace(/\px/g, '');
            }
            document.onmousemove = function (e) {
                const l = e.clientX - disX;
                const t = e.clientY - disY;
                dragDom.style.left = `${l + styL}px`;
                dragDom.style.top = `${t + styT}px`;
            }
            document.onmouseup = function () {
                document.onmousemove = null;
                document.onmouseup = null;
            }
        }
    }
})

